package com.zzw.service.impl;

import com.zzw.entity.Purchase;
import com.zzw.entity.Result;
import com.zzw.mapper.PurchaseMapper;
import com.zzw.page.Page;
import com.zzw.service.PurchaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Project: warehouse
 * @Description:
 * @Author: zzw
 */
@Service
@Slf4j
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Override
    public Result savePurchase(Purchase purchase) {
        //添加采购单
        int i = purchaseMapper.insertPurchase(purchase);
        if (i > 0) {
            return Result.ok("采购单添加成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "采购单添加失败！");
    }

    @Override
    public Page queryPurchasePage(Page page, Purchase purchase) {
        //查询采购单总行数
        int purchaseCount = purchaseMapper.selectPurchaseCount(purchase);

        //分页查询采购单
        List<Purchase> purchaseList = purchaseMapper.selectPurchasePage(page, purchase);

        //将查询到的总行数和当前页数据组装到Page对象
        page.setTotalNum(purchaseCount);
        page.setResultList(purchaseList);

        return page;
    }

    @Override
    public Result updatePurchase(Purchase purchase) {
        //根据id修改采购单
        int i = purchaseMapper.updatePurchaseById(purchase);
        if (i > 0) {
            return Result.ok("采购单修改成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "采购单修改失败！");
    }

    @Override
    public Result deletePurchase(Integer buyId) {
        //根据id删除采购单
        int i = purchaseMapper.deletePurchaseById(buyId);
        if (i > 0) {
            return Result.ok("采购单删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "采购单删除失败！");
    }

    //根据id修改预计采购数量和实际采购数量
    @Override
    public Result updatePurchaseNum(Purchase purchase) {
        int i = purchaseMapper.setNumById(purchase);
        if(i>0){
            return Result.ok("采购单修改成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"修改采购单失败");
    }
}
